import { useEffect } from "react";
import { useReactive } from "ahooks";
import { useModel } from "umi";
import { Pagination, Row, Col } from "antd";
import { Card as SemiCard, List, ButtonGroup, Button } from "@douyinfe/semi-ui";
import { IconEdit, IconRefresh } from "@douyinfe/semi-icons";

import useApplication from "../../hooks/organization/useApplication";

import ApplicationDetail from "../../components/organization_application/detail";

const AllOrganizationApplications = () => {
    const { windowInnerHeight } = useModel("global");
    const { dataLoading, applications, totalCount, loadApplications } = useApplication();

    const state = useReactive({
        selectedApplication: null,
        page: 1,
        pageSize: 20,
    });

    useEffect(() => {
        loadApplications({ pageSize: state.pageSize, page: state.page });
    }, [state.page]);

    useEffect(() => {
        if (applications?.length > 0) {
            let selectedApplication = applications.find((item) => item.appUuid === state.selectedApplication?.appUuid);
            !selectedApplication && (selectedApplication = applications[0]);
            state.selectedApplication = selectedApplication;
        }
    }, [applications]);

    const cardBodyHeight = windowInnerHeight - 90 - 54 - 20;

    return (
        <Row gutter={10} style={{ padding: 10 }}>
            <Col span={5}>
                <SemiCard
                    title={<span style={{ lineHeight: "32px", fontSize: 16 }}>应用列表 ({totalCount})</span>}
                    headerExtraContent={
                        <ButtonGroup>
                            <Button icon={<IconRefresh />} onClick={() => loadApplications({ pageSize: state.pageSize, page: state.page })}></Button>
                        </ButtonGroup>
                    }
                    headerStyle={{ padding: "10px 14px" }}
                    shadows="always"
                    bordered={false}
                    bodyStyle={{ padding: 1, height: cardBodyHeight - 46, overflow: "auto" }}
                    footerStyle={{ padding: 0 }}
                    footerLine={true}
                    footer={
                        <div style={{ padding: "11px 0", textAlign: "center" }}>
                            <Pagination
                                simple
                                total={totalCount}
                                pageSize={state.pageSize}
                                position={["none", "bottomCenter"]}
                                hideOnSinglePage={false}
                                showSizeChanger={false}
                                onChange={(page) => (state.page = page)}
                                // itemRender={(_, type, originalElement) => {
                                //     if (type === "prev") return <a>上一页</a>;
                                //     if (type === "next") return <a>下一页</a>;
                                //     return originalElement;
                                // }}
                            />
                        </div>
                    }
                >
                    <List
                        loading={dataLoading}
                        dataSource={applications}
                        renderItem={(item, index) => (
                            <List.Item
                                key={index}
                                className={`list-item ${state.selectedApplication?.appUuid === item?.appUuid ? "list-selected-item" : ""}`}
                                main={<div title={item.appDesc}>{item?.appName || "-"}</div>}
                                onClick={() => (state.selectedApplication = item)}
                            ></List.Item>
                        )}
                    />
                </SemiCard>
            </Col>
            <Col span={19}>
                <SemiCard
                    title={<span style={{ lineHeight: "32px", fontSize: 16 }}>应用信息 - {state.selectedApplication?.appName}</span>}
                    headerExtraContent={<ButtonGroup></ButtonGroup>}
                    headerStyle={{ padding: "10px 14px" }}
                    shadows="always"
                    bordered={false}
                    bodyStyle={{ padding: 10, height: cardBodyHeight, overflow: "auto" }}
                >
                    {state.selectedApplication && <ApplicationDetail item={state.selectedApplication || {}} reloadData={loadApplications} />}
                </SemiCard>
            </Col>
        </Row>
    );
};

export default AllOrganizationApplications;
